package cn.lh.util;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import java.lang.reflect.Field;
import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.ECGenParameterSpec;

public class ECCDemo {
    public static void main(String[] args) {
        try {
            Security.addProvider(new BouncyCastleProvider());

            // 生成密钥对
            KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
            setKeyLength(keyPairGenerator,256);
            ECGenParameterSpec ecGenParameterSpec = new ECGenParameterSpec("secp256r1");
            keyPairGenerator.initialize(ecGenParameterSpec, new SecureRandom());
            KeyPair keyPair = keyPairGenerator.generateKeyPair();

            // 获取私钥和公钥
            ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
            ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();

            // 要加密的明文
            String plaintext = "Hello, ECC!";

            // 加密明文
            byte[] encryptedData = encrypt(plaintext.getBytes(), publicKey);
            System.out.println("加密后的数据：" + Hex.toHexString(encryptedData));

            // 解密密文
            byte[] decryptedData = decrypt(encryptedData, privateKey);
            String decryptedText = new String(decryptedData);
            System.out.println("解密后的数据：" + decryptedText);
        } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException | InvalidKeyException |
                 NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
    }

    private static void setKeyLength(KeyPairGenerator keyPairGenerator, int keySize) {
        try {
            // 获取密钥生成器的字段
            Field field = KeyPairGenerator.class.getDeclaredField("keySize");
            field.setAccessible(true);

            // 动态设置密钥长度
            field.setInt(keyPairGenerator, keySize);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // 使用公钥加密数据
    private static byte[] encrypt(byte[] plaintext, ECPublicKey publicKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException {
        Cipher cipher = Cipher.getInstance("ECIES", "BC");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return cipher.doFinal(plaintext);
    }

    // 使用私钥解密数据
    private static byte[] decrypt(byte[] ciphertext, ECPrivateKey privateKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, NoSuchProviderException {
        Cipher cipher = Cipher.getInstance("ECIES", "BC");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        return cipher.doFinal(ciphertext);
    }
}
